package com.jin.jpa.wrapper;

import com.jin.jpa.wrapper.expression.IExpression;
import com.jin.jpa.wrapper.expression.SelectExpression;
import com.jin.jpa.wrapper.support.FieldFunction;
import com.jin.jpa.wrapper.support.IWrapper;
import com.jin.jpa.wrapper.util.LambdaUtils;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yuanxiuyao
 * @Date: 2024/1/8 16:31
 * @PackageName: org.example.jpa.wrapper.expression
 * @Version 1.0
 */
public interface AbstractSelectWrapper<T> extends IWrapper<T> {

    List<SelectExpression> selectExpressions = new ArrayList<>();

    default IWrapper<T> select(FieldFunction<T, ?> expression){
        PropertyDescriptor propertyDescriptor = LambdaUtils.extract(expression, getInstanceClass());
        selectExpressions.add(SelectExpression.of(propertyDescriptor.getName(), SelectExpression.SelectOp.NORMAL));
        return this;
    }

    default IWrapper<T> selectMin(FieldFunction<T, ?> expression){
        PropertyDescriptor propertyDescriptor = LambdaUtils.extract(expression, getInstanceClass());
        selectExpressions.add(SelectExpression.of(propertyDescriptor.getName(), SelectExpression.SelectOp.MIN));
        return this;
    }

    default IWrapper<T> selectMax(FieldFunction<T, ?> expression){
        PropertyDescriptor propertyDescriptor = LambdaUtils.extract(expression, getInstanceClass());
        selectExpressions.add(SelectExpression.of(propertyDescriptor.getName(), SelectExpression.SelectOp.MAX));
        return this;
    }

}
